# -*- sh -*-
# vi:syntax=sh
# This is a shell function library sourced by some OVN scripts.
# It is not intended to be invoked on its own.
# The code copied from ovs/utilities/ovs-lib.in

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

## ----------------- ##
## configure options ##
## ----------------- ##

# All of these should be substituted by the Makefile at build time.
ovn_logdir=${OVN_LOGDIR-'@LOGDIR@'}                 # /var/log/ovn
ovn_rundir=${OVN_RUNDIR-'@OVN_RUNDIR@'}                 # /var/run/ovn
ovn_sysconfdir=${OVN_SYSCONFDIR-'@sysconfdir@'}     # /etc
ovn_etcdir=$ovn_sysconfdir/ovn                      # /etc/ovn
ovn_datadir=${OVN_PKGDATADIR-'@pkgdatadir@'}        # /usr/share/ovn
ovn_bindir=${OVN_BINDIR-'@bindir@'}                 # /usr/bin
ovn_sbindir=${OVN_SBINDIR-'@sbindir@'}              # /usr/sbin

# /etc/ovn or /var/lib/ovn
if test X"$OVN_DBDIR" != X; then
    ovn_dbdir=$OVN_DBDIR
elif test X"$OVN_SYSCONFDIR" != X; then
    ovn_dbdir=$OVN_SYSCONFDIR/ovn
else
    ovn_dbdir='@DBDIR@'
fi

VERSION='@VERSION@'

DAEMON_CWD=/

LC_ALL=C; export LC_ALL

ovn_install_dir () {
    DIR="$1"
    INSTALL_MODE="${2:-755}"
    INSTALL_USER="$(id -un)"
    INSTALL_GROUP="$(id -gn)"
    [ "$OVN_USER" != "" ] && INSTALL_USER="${OVN_USER%:*}"
    [ "${OVN_USER##*:}" != "" ] && INSTALL_GROUP="${OVN_USER##*:}"

    if test ! -d "$DIR"; then
        install -d -m "$INSTALL_MODE" -o "$INSTALL_USER" -g "$INSTALL_GROUP" "$DIR"
        restorecon "$DIR" >/dev/null 2>&1
    fi
}

start_wrapped_daemon() {
    wrapper=$1
    daemon=$2
    priority=$3
    strace=""
    shift ; shift ; shift ;

    # wrapper
    case $wrapper in
        valgrind)
            if (valgrind --version) > /dev/null 2>&1; then
                set valgrind -q --leak-check=full --time-stamp=yes \
                    --log-file="$ovn_logdir/$daemon.valgrind.log.%p" "$@"
            else
                log_failure_msg "valgrind not installed, running $daemon without it"
            fi
            ;;
        strace)
            if (strace -V) > /dev/null 2>&1; then
                strace="strace -tt -T -s 256 -ff"
                if (strace -DV) > /dev/null 2>&1; then
                    # Has the -D option.
                    set $strace -D -o "$ovn_logdir/$daemon.strace.log" "$@"
                    strace=""
                fi
            else
                log_failure_msg "strace not installed, running $daemon without it"
            fi
            ;;
        glibc)
            set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
            ;;
        '')
            ;;
        *)
            log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
            ;;
    esac

    # priority
    if test X"$priority" != X; then
        set nice -n "$priority" "$@"
    fi

    action "Starting $daemon" "$@" || return 1

    if test X"$strace" != X; then
        # Strace doesn't have the -D option so we attach after the fact.
        setsid $strace -o "$ovn_logdir/$daemon.strace.log" \
            -p `cat $ovn_rundir/$daemon.pid` > /dev/null 2>&1 &
    fi
}

start_ovn_daemon () {
    priority=$1
    wrapper=$2
    shift; shift
    daemon=$1

    # drop core files in a sensible place
    ovn_install_dir "$DAEMON_CWD"
    set "$@" --no-chdir
    cd "$DAEMON_CWD"

    # log file
    ovn_install_dir "$ovn_logdir" "750"
    set "$@" --log-file="$ovn_logdir/$daemon.log"

    # pidfile and monitoring
    ovn_install_dir "$ovn_rundir"
    set "$@" --pidfile="$ovn_rundir/$daemon.pid"
    set "$@" --detach
    test X"$MONITOR" = Xno || set "$@" --monitor

    chown -R $INSTALL_USER:$INSTALL_GROUP $ovn_logdir
    chown -R $INSTALL_USER:$INSTALL_GROUP $ovn_rundir

    start_wrapped_daemon "$wrapper" $daemon "$priority" "$@"
}

stop_ovn_daemon () {
    local pid_file=$2
    local ctl_file=$3
    local other_args=$4

    if [ -z "$pid_file" ]; then
        pid_file="$ovn_rundir/$1.pid"
    fi

    if test -e "$pid_file"; then
        if pid=`cat "$pid_file"`; then
            if [ -z "$ctl_file" ]; then
                ctl_file="$ovn_rundir/$1.$pid.ctl"
            fi

            if pid_exists "$pid" >/dev/null 2>&1; then :; else
                rm -f $ctl_file $pid_file
                return 0
            fi

            graceful="EXIT .1 .25 .65 1"
            actions="TERM .1 .25 .65 1 1 1 1 \
                     KILL 1 1 1 2 10 15 30 \
                     FAIL"
            version=`ovn-appctl -T 1 -t $ctl_file version \
                     | awk 'NR==1{print $NF}'`

            # Use `ovn-appctl exit` only if the running daemon version
            # is >= 2.5.90.  This script might be used during upgrade to
            # stop older versions of daemons which do not behave correctly
            # with `ovn-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
            # internal ports).
            if version_geq "$version" "2.5.90"; then
                actions="$graceful $actions"
            fi
            actiontype=""
            for action in $actions; do
                if pid_exists "$pid" >/dev/null 2>&1; then :; else
                    # pid does not exist.
                    if [ -n "$actiontype" ]; then
                        return 0
                    fi
                    # But, does the file exist? We may have had a daemon
                    # segfault with `ovn-appctl exit`. Check one more time
                    # before deciding that the daemon is dead.
                    [ -e "$pid_file" ] && sleep 2 && pid=`cat "$pid_file"` 2>/dev/null
                    if pid_exists "$pid" >/dev/null 2>&1; then :; else
                        return 0
                    fi
                fi
                case $action in
                    EXIT)
                        action "Exiting $1 ($pid)" \
                            ${bindir}/ovn-appctl -T 1 -t $ctl_file exit $other_args
                        # The above command could have resulted in delayed
                        # daemon segfault. And if a monitor is running, it
                        # would restart the daemon giving it a new pid.
                        ;;
                    TERM)
                        action "Killing $1 ($pid)" kill $pid
                        actiontype="force"
                        ;;
                    KILL)
                        action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
                        actiontype="force"
                        ;;
                    FAIL)
                        log_failure_msg "Killing $1 ($pid) failed"
                        return 1
                        ;;
                    *)
                        sleep $action
                        ;;
                esac
            done
        fi
    fi
    log_success_msg "$1 is not running"
}

daemon_status () {
    pidfile=$ovn_rundir/$1.pid
    if test -e "$pidfile"; then
        if pid=`cat "$pidfile"`; then
            if pid_exists "$pid"; then
                echo "$1 is running with pid $pid"
                return 0
            else
                echo "Pidfile for $1 ($pidfile) is stale"
            fi
        else
            echo "Pidfile for $1 ($pidfile) exists but cannot be read"
        fi
    else
        echo "$1 is not running"
    fi
    return 1
}

daemon_is_running () {
    pidfile=$ovn_rundir/$1.pid
    test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
} >/dev/null 2>&1
